﻿using System;
using System.Collections;
using System.Xml;

namespace MPMovieCatalog.Domain
{
    [Serializable()]
    public class MovieInfo : MovieShortInfo
    {
        private ArrayList m_genres = new ArrayList();

        private ArrayList m_actors = new ArrayList();

        private ArrayList m_files = new ArrayList();

        public void AddActor(Actor actor)
        {
            if (m_actors == null)
            {
                m_actors = new ArrayList();
            }
            m_actors.Add(actor);
        }

        public void AddGenre(Genre genre)
        {
            if (m_genres == null)
            {
                m_genres = new ArrayList();
            }
            m_genres.Add(genre);
        }

        public void AddFile(ShareFile file)
        {
            if (m_files == null)
            {
                m_files = new ArrayList();
            }
            m_files.Add(file);
        }

        public XmlDocument ToXML()
        {
            XmlDocument movieXML = new XmlDocument();
            XmlElement root = movieXML.CreateElement("movieInfo");
            movieXML.AppendChild(root);

            XmlElement element = movieXML.CreateElement("title");
            element.AppendChild(movieXML.CreateTextNode(this.Name));
            root.AppendChild(element);

            element = movieXML.CreateElement("tagline");
            element.AppendChild(movieXML.CreateTextNode(this.Tagline));
            root.AppendChild(element);

            element = movieXML.CreateElement("description");
            element.AppendChild(movieXML.CreateTextNode(this.Description));
            root.AppendChild(element);

            element = movieXML.CreateElement("imdbRating");
            element.AppendChild(movieXML.CreateTextNode(this.IMDBRating.Replace(',','.')));
            root.AppendChild(element);

            element = movieXML.CreateElement("imdbNumber");
            element.AppendChild(movieXML.CreateTextNode(this.IMDBNumber));
            root.AppendChild(element);

            element = movieXML.CreateElement("imdbVotes");
            element.AppendChild(movieXML.CreateTextNode(this.IMDBVotes));
            root.AppendChild(element);

            element = movieXML.CreateElement("year");
            element.AppendChild(movieXML.CreateTextNode(this.Year));
            root.AppendChild(element);

            element = movieXML.CreateElement("runtime");
            element.AppendChild(movieXML.CreateTextNode(this.Runtime));
            root.AppendChild(element);

            element = movieXML.CreateElement("mpaaRating");
            element.AppendChild(movieXML.CreateTextNode(this.MPAARating));
            root.AppendChild(element);

            element = movieXML.CreateElement("director");
            element.AppendChild(movieXML.CreateTextNode(this.Director));
            root.AppendChild(element);

            element = movieXML.CreateElement("writer");
            element.AppendChild(movieXML.CreateTextNode(this.Writer));
            root.AppendChild(element);

            element = movieXML.CreateElement("picture");
            element.AppendChild(movieXML.CreateTextNode(this.PictureFileName));
            root.AppendChild(element);

            element = movieXML.CreateElement("actors");
            root.AppendChild(element);
            foreach (Actor actor in m_actors)
            {
                XmlElement child = movieXML.CreateElement("actor");
                child.AppendChild(movieXML.CreateTextNode(actor.Name));
                element.AppendChild(child);
            }

            element = movieXML.CreateElement("genres");
            root.AppendChild(element);
            foreach (Genre genre in m_genres)
            {
                XmlElement child = movieXML.CreateElement("genre");
                child.AppendChild(movieXML.CreateTextNode(genre.Name));
                element.AppendChild(child);
            }

            element = movieXML.CreateElement("files");
            root.AppendChild(element);
            foreach (ShareFile file in m_files)
            {
                XmlElement child = movieXML.CreateElement("file");
                child.AppendChild(movieXML.CreateTextNode(file.Name));
                element.AppendChild(child);
            }

            return movieXML;
        }

        public string Picture { get; set; }

        public string PictureFileName { get; set; }

        public string Thumb { get; set; }

        public string Tagline { get; set; }

        public string Description { get; set; }

        public string IMDBRating { get; set; }

        public string IMDBNumber { get; set; }

        public string IMDBVotes { get; set; }

        public string Year { get; set; }

        public string Runtime { get; set; }

        public string MPAARating { get; set; }

        public string Director { get; set; }

        public string Writer { get; set; }

        public ArrayList Genres
        {
            get { return m_genres; }
            set { m_genres = value; }
        }

        public ArrayList Actors
        {
            get { return m_actors; }
            set { m_actors = value; }
        }

        public ArrayList Files
        {
            get { return m_files; }
            set { m_files = value; }
        }
    }
}
